import java.util.Scanner;
import java.util.Arrays;
public class NGuessNumber {
    public static int a_num = 0;
    public static int b_num = 0;
    public static int[] numbers=new int[4];
    public static int[] guesslist=new int[4];

    public static void main(String[] args) {
        System.out.println("欢迎来到新猜数字小游戏！");
        System.out.println("请输入一个1-9之间的整数");
        Scanner scanner = new Scanner(System.in);
        while(true) {
            generateAnswer();
            boolean kaishi = true;
            while (kaishi) {
                getPlayerInput();
                compareGuessAnswer();
                if (isWin() == true) {
                    System.out.println("获胜");
                    break;
                }else {
                    System.out.println("错了!");
                }
            }
            System.out.println("再玩一次吗?(y/n)");
            String again = scanner.next();
            if (!again.equalsIgnoreCase("y")) {
                break;
            }
        }
    }

    public static int[] generateAnswer(){
        //随机生成4个0-9之间的不重复的整型数作为答案并返回的功能。
        int yy = 0;
        while (true) {
            int random = (int)(Math.random() * 9 + 1);
            int j = 0;
            for ( ; j < numbers.length; j++) {
                if(random == numbers[j]) {
                    break;
                }
            }
            if(j == numbers.length ) {
                numbers[yy++] = random;
            }
            if (yy == 4) {
                break;
            }
        }
        return numbers;
    }
    public static int[] getPlayerInput(){
        //接收玩家输入的4个数字并返回
        try {
            Scanner scanner = new Scanner(System.in);
            for (int i = 0; i < 4; i++) {
                System.out.println("请输入0-9之间的第" + (i+1) + "个数字：");
                int qm = scanner.nextInt();
                if (qm >= 10 || qm < 0) {
                    System.out.print("输入错误");
                    i=i-1;
                }else {
                    guesslist[i] = qm;
                }
            }
        }catch (Exception e){
            System.out.println("输入错误");
        }
        System.out.println(Arrays.toString(guesslist)+"\n");
        return guesslist;
    }
    public static String compareGuessAnswer(){
        a_num=0;
        b_num=0;
        for (int i=0;i<numbers.length;i++){
            for (int j=0;j<numbers.length;j++){
                if (i!=j){
                    if (numbers[i]==guesslist[j]){
                        b_num++;
                    }
                }else {
                    if (numbers[i]==guesslist[j]){
                        a_num++;
                    }
                }
            }
        }
        System.out.println(a_num+"A"+b_num+"B");
        return a_num+"A"+b_num+"B";
    }
    public static boolean isWin(){
        boolean qq = false;
        //根据提示信息中的A的数量判断玩家是否获胜,如果获胜返回true,否则返回false
        if (a_num==4){
            qq = true;
        }else {
            qq = false;
        }
        return qq;
    }
}
